export var APIBaseURL
// APIBaseURL = "http://localhost:10001";
// APIBaseURL = "http://lt.szrepark.com:81/lt";
//自己test
// APIBaseURL = "http://test.szrepark.com/lt";
//三明同安 没有大屏，不要三明字样
// APIBaseURL = "http://8.134.66.0/lt";
//福清
// APIBaseURL = "http://parkservice.fjrcloud.com/lt";
//融云新服务器
// APIBaseURL = "http://park.fjrcloud.com/lt";
//博云新服务器
// APIBaseURL = "http://47.107.94.229/lt";
//三明
APIBaseURL = "http://muse.cheboyi.com/lt";
//锐泊
// APIBaseURL = "http://ruibopark.com/lt";
// APIBaseURL = "http://www.ruibopark.com/lt";
// 正通
// APIBaseURL = "http://47.118.21.255/lt";
//祥荣
// APIBaseURL = "http://47.118.24.74/lt";
//车泊易 新服务
// let APIBaseURL = "http://platform.lead-parking.com/lt";
//厦门城市引擎
// let APIBaseURL = "http://39.103.220.194/lt";
//升级服务器
// APIBaseURL = "http://platform.lead-parking.com/lt";
// 穗腾
// APIBaseURL = "http://platform.lead-parking.com/ltst";
export var titles
if (APIBaseURL === "http://muse.cheboyi.com/lt" || APIBaseURL ==="http://muse.cheboyi.com/lt_beta") {
    titles = '三明智慧停车管理平台'
}
if (APIBaseURL === "http://park.fjrcloud.com/lt") {
    titles = '融云智慧停车管理平台'
}
if (APIBaseURL === "http://test.szrepark.com/lt" || APIBaseURL === "http://localhost:10001") {
    titles = '测试智慧停车管理平台'
}
if (APIBaseURL === "http://parkservice.fjrcloud.com/lt") {
    titles = '福清智慧停车管理平台'
}
if (APIBaseURL === "http://47.118.21.255/lt") {
    titles = '正通智慧停车管理平台'
}
if (APIBaseURL === "http://47.118.24.74/lt") {
    titles = '祥荣智慧停车管理平台'
}
if (APIBaseURL === "http://ruibopark.com/lt" || APIBaseURL === "http://www.ruibopark.com/lt") {
    titles = '锐泊智慧停车管理平台'
}
if (APIBaseURL === "http://xmsuiteng.com/lt") {
    titles = '穗腾智慧停车管理平台'
}
if (APIBaseURL === "http://47.107.94.229/lt") {
    titles = '博云智泊智慧停车管理平台'
}

let production = false;
if (process.env.NODE_ENV === "development") {
    //     // APIBaseURL = "http://192.168.0.147:10001/";
    //     APIBaseURL = "http://localhost:10001";
    //     // APIBaseURL = "http://lt.szrepark.com:81/lt";
} else {
    //     // APIBaseURL = "http://www.pxparking.com/api/";
    //     APIBaseURL = "http://localhost:10001";
    //     // APIBaseURL = "http://lt.szrepark.com:81/lt";
    production = true;
}

import Vue from 'vue';
import Qs from 'qs';
import '../utils/instance'

export default {
    requestGet(url, data) {
        let param = this.convertToString(data);
        let config = this.generateConfig(url, param);
        return this.baseRequest(config);
    },

    requestDownloadGet(url, data) {
        let param = this.convertToString(data);
        let responseType = 'blob';
        let config = this.generateConfig(url, param, 0, false, responseType);
        return this.downloadRequest(config);

    },
    requestPost(url, data) {
        data = Qs.stringify(data);
        let config = this.generateConfig(url, data, 1, false);
        return this.baseRequest(config);
    },
    requestPostForJson(url, data) {
        let config = this.generateConfig(url, data, 1, true);
        return this.baseRequest(config);
    },

    downloadRequest(config) {
        return new Promise((resolve, reject) => {
            Vue.axios(config).then(res => {
                resolve(res);
            }).catch(err => {
                reject({ data: err })
            })
        })
    },

    baseRequest(config) {
        return new Promise((resolve, reject) => {
            Vue.axios(config).then(res => {
                if (res.data.code != 2000) {
                    reject({ data: res.data.msg, code: res.data.code, param: config.data });
                    return;
                }
                resolve({ data: res.data.data, code: res.data.code })
            }).catch(err => {
                reject({ data: err, param: config.data })
            })
        })
    },

    /**
     * 生成 请求配置
     * @param url
     * @param data
     * @param methodSign 请求方式 0 get 1 post
     * @param json true 使用json 传输数据
     * @param responseType 设置响应类型
     */
    generateConfig(url, data, methodSign, json, responseType) {
        let config = {};
        let method = 'POST';
        if (production) {
            url = APIBaseURL + url;
        } else {
            url = APIBaseURL + url;
            // url = 'proxy' + url;
        }
        if (methodSign === 1) {
            config.data = data;
        } else {
            method = 'GET';
            url = url + data;
        }
        config.method = method;
        config.url = url;
        //指定传输的数据类型
        if (json) {
            config.headers = { 'Content-Type': 'application/json' };
        }
        //设置响应类型
        if (responseType) {
            config.responseType = responseType;
        }
        return config;
    },

    convertToString(data) {
        if (!data) return ""
        let result = "",
            isFirst = true;
        for (let i in data) {
            result += (isFirst ? "?" : "&")
            result += (i + "=" + data[i]);
            isFirst = false;
        }
        return result;
    },
}
